package com.jiaai.web.zfp_back.live;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.common.api.model.RPage;
import com.jiaai.exception.ServerException;
import com.jiaai.gift.api.IGiftHisApi;
import com.jiaai.live.api.IAnchorApi;
import com.jiaai.live.api.ILiveApi;
import com.jiaai.live.model.BLiveVO;
import com.jiaai.live.model.LiveQueryVO;
import com.jiaai.util.RestResultGenerator;
import com.jiaai.util.rongyun.msgtype.RYMsgType;
import com.jiaai.util.rongyun.rong.messages.TxtMessage;
import com.jiaai.util.rongyun.rong.models.CodeSuccessResult;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
 * class_name:ZBLiveController
 * describe:TODO
 * create_user:张昉平
 * create_date:2017/12/20
 * create_date:10:09
 */
@RestController
@RequestMapping("/api/v1/admin/live")
public class ZBLiveController {

    @Reference
    private ILiveApi iLiveApi;
    @Reference
    private IGiftHisApi iGiftHisApi;
    @Reference
    private IAnchorApi iAnchorApi;

    @GetMapping("/counthead")
    public Object countTop10()throws ServerException {
        return  RestResultGenerator.genSuccessResult(iLiveApi.countHead());
    }

    @GetMapping("/list")
    public Object getAll(@RequestParam Integer curr, LiveQueryVO vo)throws ServerException {
        return  RestResultGenerator.genSuccessResult(iLiveApi.getAll(curr, vo));
    }

    @GetMapping("/allLive")
    public Object allLive(@RequestParam Integer curr, LiveQueryVO vo)throws ServerException {
        return  RestResultGenerator.genSuccessResult(iLiveApi.allLive(curr, vo));
    }
    @GetMapping("/oneInfo")
    public Object oneInfo(@RequestParam String anchorId, @RequestParam Integer liveId){
        Map map = iAnchorApi.oneInfo(anchorId);
        CountDownLatch countDownLatch = new CountDownLatch(3);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    map.put("sendGift", iGiftHisApi.getOneAllHis(map.get("mId").toString(), 1));//送出去的礼物
                }finally {
                    countDownLatch.countDown();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    map.put("getGift", iGiftHisApi.getOneAllHis(anchorId, 2));//收到的礼物
                }finally {
                    countDownLatch.countDown();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    map.put("phoneConfig", iLiveApi.getPhoneConfig(liveId));
                }finally {
                    countDownLatch.countDown();
                }
            }
        }).start();

        return RestResultGenerator.genSuccessResult(map);
    }

    @PostMapping("/editWatch")
    public Object editWatch(@RequestParam Integer liveId, @RequestParam Integer watchNum){
        iLiveApi.editWatchNum(liveId, watchNum);
        return RestResultGenerator.genSuccessResult("修改成功");
    }

    /**
     * 监控
     * @return
     */
    @GetMapping("/jiankong")
    public Object getJK(@RequestParam Integer curr, LiveQueryVO vo){
        RPage rPage = (RPage) iLiveApi.getJK(curr, vo);
        CountDownLatch countDownLatch = new CountDownLatch(rPage.getRows().size());
        for (Object e : rPage.getRows()){
            Map ee = (Map) e;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Map<String,Object> info = iLiveApi.getLiveInfo((Integer) ee.get("liveId"),"");
                        ee.put("info", info);
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }finally {
                        countDownLatch.countDown();
                    }
                }
            }).start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return RestResultGenerator.genSuccessResult(rPage);
    }

    @GetMapping("/pullstream/{id}")
    public Object getPullStream(@PathVariable("id") Integer id){
        try {
            return RestResultGenerator.genSuccessResult(iLiveApi.getLiveInfo(id,""));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("服务器挂了!");
        }
    }


    /**
     * 监控,--->警告或停播
     * @param anchorId
     * @param type -1:停播 -2:警告
     * @return
     */
    @PostMapping("/jk")
    public Object warn(@RequestParam String anchorId, @RequestParam Integer type, @RequestParam(required = false, defaultValue = "") String content){
        String[] messagePublishChatroomToChatroomId = {anchorId};
        TxtMessage messagePublishChatroomTxtMessage = null;
        if (type == -2){
            messagePublishChatroomTxtMessage = new TxtMessage(content, ""+RYMsgType.WARN);
        }else if (type == -1){
            messagePublishChatroomTxtMessage = new TxtMessage("STOP", ""+RYMsgType.STOP_LIVE);
        }

        CodeSuccessResult messagePublishChatroomResult = null;
        try {
//            messagePublishChatroomResult = RongYunUtil.getInstance().message.publishChatroom(
//                    "admin", messagePublishChatroomToChatroomId, messagePublishChatroomTxtMessage);
            if (messagePublishChatroomResult.getCode() == 200){
                return RestResultGenerator.genSuccessResult("成功");
            }
            return RestResultGenerator.genErrorResult("通讯失败");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("通讯失败");
        }
    }

    @GetMapping("/tradeXLK")
    public Object tradeXLK(){
        return RestResultGenerator.genSuccessResult(iLiveApi.tradeXLK());
    }

    @PostMapping("/editLive")
    public Object editLive(@RequestBody BLiveVO bLiveVO) throws Exception {
        try {
            iLiveApi.edit(bLiveVO);
            return RestResultGenerator.genSuccessResult("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加失败");
        }
    }

    @GetMapping("/finINfor")
    public Object finINfor(@RequestParam("mId") String mId,@RequestParam("liveId") Integer liveId)throws ServerException {
        try {
            return  RestResultGenerator.genSuccessResult(iLiveApi.finINfor(mId,liveId));
        } catch (Exception e) {
            throw new RuntimeException("查询失败");
        }
    }
}
